#!/bin/bash
# https://igread.coding.net/p/my/d/shell/git/raw/master/module/containerd.sh


containerd_ver=""

function containerd_install {

    function axel_install() {
        if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
            yum -y install gcc openssl openssl-devel
            local latestver
            latestver=$(curl -s https://api.github.com/repos/axel-download-accelerator/axel/releases/latest | grep tag_name | cut -d '"' -f 4)
            local latestver2
            latestver2=$(echo -e "${latestver}" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')
            local ufile
            ufile=axel-${latestver2}.tar.gz
            local down_url
            down_url=https://github.com/axel-download-accelerator/axel/releases/download/${latestver}/axel-${latestver2}.tar.gz

            [ ! -d "/tmp/my_tmp/axel" ] && mkdir -p "/tmp/my_tmp/axel"
            curl -fLk --retry 3 "${down_url}" -o /tmp/my_tmp/axel/"${ufile}" --create-dirs
            echo -e "$(ls /tmp/my_tmp/axel)"
            cd /tmp/my_tmp/axel && tar -zxvf "${ufile}" -C /tmp/my_tmp/axel
            cd axel-"${latestver2}" || exit
            ./configure && make && make install
            rm -rf /tmp/my_tmp/axel/
            cd ~ || exit

        elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
            apt install -y axel

        fi
    }

    if ! which axel 2>/dev/null; then
        axel_install
    else
        echo axel已经存在
    fi

    [ -z "${containerd_ver}" ] && containerd_ver=1.4.3

    containerd_url=https://github.uon.workers.dev/https://github.com/containerd/containerd/releases/download/v${containerd_ver}/containerd-${containerd_ver}-linux-amd64.tar.gz
    containerd_tmp=/tmp/my_tmp/containerd
    [ ! -d ${containerd_tmp} ] && mkdir -p ${containerd_tmp}
    if which axel &>/dev/null; then
        cd ${containerd_tmp} && axel -n 8 -ak "${containerd_url}"
    else
        cd ${containerd_tmp} && curl -fL --retry 3 -O "${containerd_url}" --create-dirs
    fi

    cd "${containerd_tmp}" || exit
    tar -xzvf containerd-${containerd_ver}-linux-amd64.tar.gz -C /usr/local

    cat >/lib/systemd/system/containerd.service <<EOF

# Copyright The containerd Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target

[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/containerd

Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity
# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity
OOMScoreAdjust=-999

[Install]
WantedBy=multi-user.target

EOF

    #创建Containerd默认配置
    [ ! -d /etc/containerd ] && mkdir -p /etc/containerd
    containerd config default >/etc/containerd/config.toml

    # 启动服务
    systemctl daemon-reload
    systemctl start containerd.service
    #systemctl stop containerd.service

    # 检测安装工具
    # runc -v
    # ctr -v
}

function containerd_remove {
    systemctl stop containerd.service
    systemctl disable containerd.service
    rm -f /etc/containerd/config.toml
    rm -f /usr/local/bin/containerd
    rm -f /usr/local/bin/containerd-shim*
    rm -f /usr/local/bin/containerd-stress
    rm -f /usr/local/bin/ctr

}

while
    getopts :v:ir varname
do
    case $varname in

    v)
        echo "$varname"
        containerd_ver=$OPTARG #将选项参数赋值给
        ;;

    i)
        # echo "$varname"
        containerd_install
        exit 0
        ;;

    r)
        echo "$varname"
        containerd_remove
        exit 0
        ;;

    :)                                                  #当选项后面没有参数时，varname的值被设置为（：），OPTARG的值被设置为选项本身
        echo "the option -$OPTARG require an arguement" #提示用户此选项后面需要一个参数
        exit 1
        ;;

    ?)                                  #当选项不匹配时，varname的值被设置为（？），OPTARG的值被设置为选项本身
        echo "Invaild option: -$OPTARG" #提示用户此选项无效
        echo "-v <kernel version> "
        exit 2
        ;;

    esac
done
